﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Hull2D.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2007, 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Collision.
//
//   starLiGHT.Collision is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Collision is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Collision. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Collision are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 3091                  $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2011-01-07 11:43:07 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Collision
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    #endregion

    public enum PolygonType
    {
        UNKNOWN,
        INVALID,
        CONVEX,
        CONCAVE,
    }

    public class Hull2D
    {
        private Vector2[] vertices;
        private int vertexIndex;
        private PolygonType type;
        private Vector2? center;
        private List<Vector2> edges = new List<Vector2>();

        public Hull2D()
        {
            this.vertices = new Vector2[3];
            this.type = PolygonType.UNKNOWN;
        }

        public Hull2D(params Vector2[] points)
        {
            if (points == null)
            {
                throw new ArgumentNullException("points");
            }

            this.vertices = new Vector2[points.Length];
            Array.Copy(points, this.vertices, points.Length);
            this.vertexIndex = points.Length;
            this.type = PolygonType.UNKNOWN;
        }

        public int VertexCount
        {
            get { return this.vertices.Length; }
        }

        public int EdgeCount
        {
            get 
            {
                if (this.edges.Count == 0)
                {
                    this.BuildEdges();
                }

                return this.edges.Count; 
            }
        }

        public IEnumerable<Vector2> Vertices
        {
            get
            {
                for (int i = 0; i < this.vertexIndex; i++)
                {
                    yield return this.vertices[i];
                }
            }
        }

        public IEnumerable<Vector2> Edges
        {
            get
            {
                if (this.edges.Count == 0)
                {
                    this.BuildEdges();
                }

                for (int i = 0; i < this.edges.Count; i++)
                {
                    yield return this.edges[i];
                }
            }
        }

        public bool IsConvex
        {
            get
            {
                if (this.type == PolygonType.UNKNOWN)
                {
                    this.type = this.Convex(this.vertices, this.vertices.Length);
                }

                return this.type == PolygonType.CONVEX;
            }
        }

        public bool IsValid
        {
            get
            {
                if (this.type == PolygonType.UNKNOWN)
                {
                    this.type = this.Convex(this.vertices, this.vertices.Length);
                }

                return this.type != PolygonType.INVALID && this.type != PolygonType.UNKNOWN;
            }
        }

        public bool IsConcave
        {
            get
            {
                if (this.type == PolygonType.UNKNOWN)
                {
                    this.type = this.Convex(this.vertices, this.vertices.Length);
                }

                return this.type == PolygonType.CONCAVE;
            }
        }

        public Vector2 Center
        {
            get
            {
                if (!this.center.HasValue)
                {
                    Vector2 sum = Vector2.Zero;
                    foreach (Vector2 v in this.Vertices)
                    {
                        sum += v;
                    }

                    this.center = sum / this.VertexCount;
                }

                return this.center.Value;
            }
        }

        public Vector2 this[int index]
        {
            get { return this.vertices[index]; }
            set { this.vertices[index] = value; }
        }

        public Vector2 GetEdge(int index)
        {
            if (this.edges.Count == 0)
            {
                this.BuildEdges();
            }

            return this.edges[index];
        }

        public void AddVertex(Vector2 v)
        {
            if (this.vertexIndex >= this.vertices.Length)
            {
                Vector2[] tmp = (Vector2[])this.vertices.Clone();
                this.vertices = new Vector2[tmp.Length * 2];
                tmp.CopyTo(this.vertices, 0);
            }

            this.vertices[this.vertexIndex++] = v;

            this.type = PolygonType.UNKNOWN;
        }

        private void BuildEdges()
        {
            Vector2 p1;
            Vector2 p2;
            this.edges.Clear();

            for (int i = 0; i < this.vertices.Length; i++)
            {
                p1 = this.vertices[i];
                if (i + 1 >= this.vertices.Length)
                {
                    p2 = this.vertices[0];
                }
                else
                {
                    p2 = this.vertices[i + 1];
                }

                this.edges.Add(p2 - p1);
            }
        }

        private PolygonType Convex(Vector2[] p, int n)
        {
            int i, j, k;
            int flag = 0;
            float z;

            if (n < 3)
            {
                return PolygonType.INVALID;
            }

            for (i = 0; i < n; i++)
            {
                j = (i + 1) % n;
                k = (i + 2) % n;
                z = (p[j].X - p[i].X) * (p[k].Y - p[j].Y);
                z -= (p[j].Y - p[i].Y) * (p[k].X - p[j].X);

                if (z < 0)
                {
                    flag |= 1;
                }
                else if (z > 0)
                {
                    flag |= 2;
                }

                if (flag == 3)
                {
                    return PolygonType.CONCAVE;
                }
            }

            if (flag != 0)
            {
                return PolygonType.CONVEX;
            }

            return PolygonType.INVALID;
        }
    }
}
